/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "config.h"
#include "header.h"
#include "channel.h"

Config::Config (QWidget * parent, const char *name):
QVBox (parent, name, WType_Dialog)
{
  QSettings settings;


  QHBox *hbox = new QHBox (this);
  QLabel *label;
  label = new QLabel ("Player:", hbox);
  editplayer =
    new QLineEdit (settings.
		   readEntry ("/qsopcast/player",
			      "mplayer -ontop -geometry 100%:100%"), hbox);

  hbox = new QHBox (this);
  label = new QLabel ("Channels url:", hbox);
  editchannelurl =
    new QLineEdit (settings.
		   readEntry ("/qsopcast/channelurl",
			      "http://channel.sopcast.com/gchlxml"), hbox);

  hbox = new QHBox (this);
  label = new QLabel ("Channels url header:", hbox);
  editchannelheader =
    new QLineEdit (settings.
		   readEntry ("/qsopcast/channelheader", "sop://"), hbox);

  hbox = new QHBox (this);
  label = new QLabel ("Record directory:", hbox);
  editrecorddir =
    new QLineEdit (settings.
		   readEntry ("/qsopcast/recorddir",
			      QDir::homeDirPath () + "/SOPCAST/"), hbox);

  radionullchannel = new QRadioButton (this);
  radionullchannel->setText ("show null channels");
  radionullchannel->setChecked (settings.
				readBoolEntry ("/qsopcast/shownullchannels",
					       FALSE));
  connect (radionullchannel, SIGNAL (toggled (bool)), this,
	   SLOT (onRadioNullChannelToggled (bool)));

  radiochannelsort = new QRadioButton (this);
  radiochannelsort->setText ("enable channel sorting");
  radiochannelsort->setChecked (settings.
				readBoolEntry
				("/qsopcast/enablechannelsorting", FALSE));

  hbox = new QHBox (this);
  label = new QLabel ("Inport:", hbox);
  labelinport = new QLabel (hbox);
  label = new QLabel ("Outport:", hbox);
  labeloutport = new QLabel (hbox);

  hbox = new QHBox (this);
  QPushButton *buttonsave = new QPushButton ("Save", hbox);
  QPushButton *buttonclose = new QPushButton ("Close", hbox);
  connect (buttonclose, SIGNAL (clicked ()), this, SLOT (close ()));
  connect (buttonsave, SIGNAL (clicked ()), this,
	   SLOT (onbuttonsaveclicked ()));

  adjustSize ();

}

void
Config::onbuttonsaveclicked ()
{
  QSettings settings;
  settings.writeEntry ("/qsopcast/player", editplayer->text ());
  settings.writeEntry ("/qsopcast/channelurl", editchannelurl->text ());
  settings.writeEntry ("/qsopcast/channelheader", editchannelheader->text ());
  settings.writeEntry ("/qsopcast/recorddir", editrecorddir->text ());
  settings.writeEntry ("/qsopcast/shownullchannels",
		       radionullchannel->isOn ());
  settings.writeEntry ("/qsopcast/enablechannelsorting",
		       radiochannelsort->isOn ());
  close ();
}

void
Config::onRadioNullChannelToggled (bool t)
{
  QListViewItem *group = channel->firstChild ();
  while (group)
    {
      QListViewItem *child = group->firstChild ();
      int count = 0;
      while (child)
	{
	  if (!child->isEnabled ())
	    {
	      if (t)
		child->setVisible (TRUE);
	      else
		child->setVisible (FALSE);
	    }
	  else
	    count++;
	  child = child->nextSibling ();
	}

      if (count == 0 && !t)
	group->setVisible (FALSE);

      group = group->nextSibling ();
    }
}
